package org.abacus.client.ui.view.login;

import org.abacus.client.ui.view.FormView;
import org.abacus.client.ui.view.ViewConfigHelper;
import org.abacus.dao.DaoFactory;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Text;


public class LoginView extends FormView {
	private static String serverUrl;

	Text serverUrlTtext;

	Text email;

	Text password;

	public static String getServerUrl() {
		return serverUrl;
	}

	@Override
	protected void initView() {
		super.initView();

		GridLayout gridLayout = null;

		shell.setText("登录");
		Group group = new Group(shell, SWT.NONE);
		{
			gridLayout = new GridLayout();
			gridLayout.numColumns = 2;
			gridLayout.makeColumnsEqualWidth = false;

			gridLayout.verticalSpacing = 20;

			group.setLayout(gridLayout);
			group.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
		}
		group.setText("登录信息");

		Label label = null;
		Text text = null;

		label = new Label(group, SWT.PUSH);
		label.setText("服务器地址:");
		label.setLayoutData(new GridData(SWT.LEFT, SWT.CENTER, false, false));

		serverUrlTtext = new Text(group, SWT.BORDER);
		serverUrlTtext.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, true));
		serverUrlTtext.setText("http://127.0.0.1:8080/chaoyangli.abacus/api");

		label = new Label(group, SWT.PUSH);
		label.setText("用户名:");
		label.setLayoutData(new GridData(SWT.LEFT, SWT.CENTER, false, false));

		text = new Text(group, SWT.BORDER);
		text.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, true));
		text.setText("179084436@qq.com");
		setScrollable("email", text);

		label = new Label(group, SWT.PUSH);
		label.setText("密码:");
		label.setLayoutData(new GridData(SWT.LEFT, SWT.CENTER, false, false));

		text = new Text(group, SWT.BORDER | SWT.PASSWORD);
		text.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, true));
		text.setText("1");
		setScrollable("password", text);

		Button button = null;

		Composite composite = new Composite(shell, SWT.NONE);
		{
			gridLayout = new GridLayout();
			gridLayout.numColumns = 2;
			gridLayout.makeColumnsEqualWidth = true;
			composite.setLayout(gridLayout);
			composite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, false, false));
		}
		button = new Button(composite, SWT.PUSH);
		button.setText("取消");
		button.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false));
		button.addListener(SWT.Selection, new Listener() {
			@Override
			public void handleEvent(Event arg0) {
				cancel();
			}
		});

		ok = button = new Button(composite, SWT.PUSH);
		button.setText("确定");
		button.setLayoutData(new GridData(SWT.CENTER, SWT.CENTER, true, true));
		button.addListener(SWT.Selection, new Listener() {
			@Override
			public void handleEvent(Event arg0) {
				ok();
			}
		});
		centerShell(shell, 350, 260);
		shell.pack();
		shell.open();
		ViewConfigHelper viewConfigHelper = ViewConfigHelper.getInstance();
		DaoFactory.getTransactionStrategy().begin();
		viewConfigHelper.createViewConfig((Composite) shell, 0, null);
		DaoFactory.getTransactionStrategy().commit();
	}

	private Button ok;

	@Override
	public void ok() {
		ok.setEnabled(false);
		try {
			serverUrl = serverUrlTtext.getText();
			getActionRequest().done(this);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	@Override
	public void cancel() {
		ok.setEnabled(true);
	}

	@Override
	public void dispose() {
		getShell().dispose();
	}

}
